﻿using IBLL;
using IDAL;
using Model;

namespace BLL
{
    public class UserBLL:IUserBLL
    {
        public IUserDAL<Logth> Ldal { get; }
        public IUserDAL<User> Udal { get; }

        public UserBLL(IUserDAL<Logth> ldal,IUserDAL<User> udal)
        {
            Ldal = ldal;
            Udal = udal;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="m">用户信息表</param>
        /// <returns></returns>
        public async Task<int> AddA(Logth m)
        {
            return await Ldal.AddAsync(m);
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="acction">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public Logth Logth(string acction, string pwd)
        {
            var list=Ldal.GetAllAsync().FirstOrDefault(x=>x.LogthName==acction);
            if(list==null)
            {
                list=new Logth() { LogthId=-1};
            }
            else
            {
                if (list.LogthPwd != pwd)
                {
                    list = new Logth() { LogthId = -2 };
                }
            }
            return list;
        }
            /// <summary>
        /// 添加
        /// </summary>
        /// <param name="m">房态信息表</param>
        /// <returns></returns>
        public async Task<int> Add(User m)
        {
            return await Udal.AddAsync(m);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">页容量</param>
        /// <param name="name">房态名称</param>
        /// <param name="num">房态编号</param>
        /// <returns></returns>
        public ShowDTO Show(int PageIndex, int PageSize, string? name, int? num)
        {
            var list = Udal.GetAllAsync();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x=>x.Username.Contains(name));
            }
            if(num>0)
            {
                list = list.Where(x => x.UserNUm==num);
            }
            ShowDTO showDTO = new ShowDTO();
            showDTO.count = list.Count();
            showDTO.users = list.OrderBy(x => x.UserId).Skip((PageIndex-1)*PageSize).Take(PageSize).ToList();
            return showDTO;
        }
        /// <summary>
        /// 删除逻辑
        /// </summary>
        /// <param name="uid">主键id</param>
        /// <returns></returns>
        public async Task<int> del(int uid)
        {
            var a = Udal.GetAllAsync().FirstOrDefault(x=>x.UserId==uid);
            return await Udal.DeleteAsync(a);
            
        }
    }
}
